package com.drojian.demo2.fragement;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.drojian.demo.R;

public class DemoActivity1 extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.part2_activity_demo1);

        Button replace = findViewById(R.id.replace);
        replace.setOnClickListener(this);
        Button change = findViewById(R.id.change);
        change.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.replace:
                Bundle bundle = new Bundle();
                bundle.putString("message", "我在activity");
                BlankFragment1 blankFragment1 = new BlankFragment1();
                blankFragment1.setArguments(bundle);
                blankFragment1.setMyFragmentCallBack(new MyFragmentCallBack() {
                    @Override
                    public void sendMsgToActivity(String mes) {
                        Toast.makeText(DemoActivity1.this, mes, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public String getMsgToActivity(String msg) {
                        return null;
                    }
                });
                replaceFragment(blankFragment1);
                break;
            case R.id.change:
                replaceFragment(new ItemFragment());
                break;
        }
    }

    public void replaceFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.frameLayout, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }


}